<?php
require_once 'guest.php';
require_once 'admin.php';
require_once 'supper_admin.php';
require_once 'child_supper_admin.php';
require_once 'supper_guest.php';
 class Dt_auth {
     protected $roles =array();
     protected  $CI;
     protected $temp = array();
     protected $temp_cp;
     protected $sort_reuslt;
     public function __construct() {
         $this->CI =& get_instance();
         $this->run();
     }
     public function run(){
         $classes = $this->include_class();
         foreach ($classes as $class) {
             $name =  str_replace('.php','',$class);
             $Class = ucfirst($name);
             if($Class!='Dt_auth'){
                 $object = new $Class();
                 if(get_parent_class($object)==null){
                    //$this->roles[$object->getUser_level()] = $object->getUser_access();
                    $this->temp[] =  array($object,$name,null);
                 }else {
                    $this->temp[] =  array($object,$name,  strtolower(get_parent_class($object)));
                 }
             }
         }
         $this->sort();
         foreach ($this->sort_reuslt as $val) {
            $object = $val[0];
            // is not absolute parent
            if($val[2]!=null){
                $parrent_object = $this->find_parrent_object_by_role_name($val[2]);
                $object->setParent_role_object($parrent_object);
             }
             $this->roles[$object->getUser_level()] = $object->getUser_access();
         }
     }
     public function get_all_roles(){
         return $this->roles;
     }
     
     private function include_class(){
        $dir = dirname(__FILE__);
        $this->CI->load->helper('directory');
        $map = directory_map($dir,TRUE);
        return $map;
     }
     // instan must sort, because parent class must be instan befor extend class instan
     private function sort(){
         $this->temp_cp = $this->temp;
         //sort
         foreach ($this->temp_cp  as  $key => $val) {//print_r($val);
            if($val[2]==null) { //is absolute parent, not  extend any role
               $this->sort_reuslt[] = $val;
               $this->find_child($val);   
            }
         }
         //print_r($this->sort_reuslt);
     }
    
     private function find_child($element){ //find child of element
         if($element!=null){
             foreach ($this->temp_cp as $key => $val) {
                 if($val[2]==$element[1]){ // child of element
                     $this->sort_reuslt[] = $val;
                     unset($this->temp_cp[$key]);
                     $this->find_child($val);
                 } 
             }
         }
     }
     
     private function find_parrent_object_by_role_name($name){
         foreach ($this->temp as $val) {
             if($val[1]==$name){
                 return $val[0];
             }
         }
         return null;
     }
 }
?>
